﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using FanaticRock.Helpers;
using FanaticRock.Interfaces;

namespace FanaticRock.Extensions
{
    public static class ObservableCollectionExtension
    {

        ///<summary>将具有下标的集合进行更新。</summary>
        public static ObservableCollection<T> UpdateIndex<T>(this ObservableCollection<T> collection) where T : IHasIndex
        {
            ObservableCollectionHelper.UpdateIndex(collection);
            return collection;
        }

        public static ObservableCollection<T> AddRange<T>(this ObservableCollection<T> collection, IEnumerable<T> range)
        {
            if (range == null) return collection;
            if (range.Count() == 0) return collection;

            foreach (var item in range)
            {
                collection.Add(item);
            }

            return collection;
        }

        public static async Task< ObservableCollection<T>> AddRangeAsnyc<T>(this ObservableCollection<T> collection, IEnumerable<T> range)
        {
            if (range == null) return collection;
            if (range.Count() == 0) return collection;

            foreach (var item in range)
            {
                await Task.Delay(100);
                collection.Add(item);
            }

            return collection;
        }
    }
}
